package by.averianov.discounta.webapp.page.shop.shopcart;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import org.apache.wicket.Page;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow.PageCreator;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.model.ResourceModel;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.util.string.StringValue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import by.averianov.discounta.datamodel.Card;
import by.averianov.discounta.datamodel.CardType;
import by.averianov.discounta.datamodel.Product;
import by.averianov.discounta.datamodel.Shop;
import by.averianov.discounta.datamodel.UserProfile;
import by.averianov.discounta.services.SaleService;
import by.averianov.discounta.services.UserService;
import by.averianov.discounta.webapp.app.BasicAuthenticationSession;
import by.averianov.discounta.webapp.page.BaseLayout;
import by.averianov.discounta.webapp.page.administrator.Administrator;
import by.averianov.discounta.webapp.page.homepage.HomePage;

public class ShopCart extends BaseLayout {
	
	private static final Logger LOGGER = LoggerFactory.getLogger(ShopCart.class);

	private Long id;
	private Product product;
	private UserProfile sessionProfile;
	private DropDownChoice<String> productCountDropDown, userCardDropDown;
	//private MyModel _myModel = new MyModel();
	
	private PageCreator pageCreator;

	public ShopCart(Product product, PageCreator pageCreator) {
		super(null);
		this.pageCreator = pageCreator;
		this.product = product;
	}

	@Inject
	private SaleService saleService;

	@Inject
	private UserService userService;


	@Override
	protected void onInitialize() {
		super.onInitialize();
		setOutputMarkupId(true);
		if(BasicAuthenticationSession.get().isSignedIn()) {
			this.sessionProfile = BasicAuthenticationSession.get().getUserProfile();
		} else {
			HomePage page = new HomePage();
			page.error(new ResourceModel("p.error.nouserlogin").getObject());
			setResponsePage(page);			
		}
		
		/*List<String> countProduct = new ArrayList<String>();
		Long i = 0L;
		do {
			countProduct.add(i.toString());
			i++;
		} while (i != product.getCountProduct());*/
		
		List<Card> userCard = userService.getAllCardByUser(sessionProfile.getId());
		List<String> numberUserCards = new ArrayList<String>();
        for (Card nUC : userCard) {
        	numberUserCards.add(nUC.getId() + " - " + nUC.getCardType()); 
        	LOGGER.debug("Add Email to userList: " + nUC.getId());
        }
        /*if(userEmailList.size() == 0) {
        	
        } */

		/*final DropDownChoice<String> productCountDropDown = new DropDownChoice<String>("productCount", new PropertyModel<String>(this, "0"), countProduct);*/
		//final DropDownChoice<String> userCardDropDown = new DropDownChoice<String>("userDiscountCard", new PropertyModel<String>(this, " "), numberUserCards);
		
		
		/*userCardDropDown.add(new AjaxFormComponentUpdatingBehavior("onchange") {
			@Override
			protected void onUpdate(AjaxRequestTarget target) {
				
				Page page = new ShopCart(product, pageCreator);
				page.error(new ResourceModel("p.error.clearuserbase").getObject());
				setResponsePage(page);
				target.addComponent(null);
			}
		});*/
	
		Form<?> form = new Form<Void>("ShopCartForm") {
			@Override
			protected void onSubmit() {
				
				PageParameters pageParameters = new PageParameters();
				
				//pageParameters.add("nameShop", nameShopText.getModelObject());
				StringValue nameShopValue = pageParameters.get("nameShop");
				
				
		        LOGGER.debug("Set Shop");
		        //Shop shop = userService.createShop(id, nameShopValue.toString());
		        //userService.saveOrUpdateShop(shop);
				setResponsePage(pageCreator.createPage());
			}
		};		 
		add(form);
		
		
		productCountDropDown = getProductCountDropDown(form);
		//userCardDropDown = getPhoneModelDDC(form);
		
		
		
		form.add(productCountDropDown);
		form.add(userCardDropDown);
		form.add(new Label("labelCreateShop", getLabelCreateShop(id)));
		form.add(new Label("labelShopIdText", getLabelShopId()));		
		final Label labelShopId = new Label("labelShopId", id);
		form.add(labelShopId);			
		form.add(new Label("labelNameShop", getLabelNameShop()));	
		form.add(new Label("labelSave", getButtonSave()));	
		
		Link linkCancel = new Link("linkCancel"){
			@Override
			public void onClick() {
				setResponsePage(pageCreator.createPage());
				
			}
		};
		linkCancel.add(new Label("labelCancel", getButtonCancel()));
		form.add(linkCancel);
		
	}
	
	private DropDownChoice<String> getProductCountDropDown(final Form form) {
		List<String> countProduct = new ArrayList<String>();
		Long i = 0L;
		do {
			countProduct.add(i.toString());
			i++;
		} while (i != product.getCountProduct());
        DropDownChoice<String> newProductCountDropDown = new DropDownChoice<String>("productCount", new PropertyModel<String>(this, "0"), countProduct);
        // Add Ajax Behaviour...
        newProductCountDropDown.add(new AjaxFormComponentUpdatingBehavior("onchange") {
            protected void onUpdate(AjaxRequestTarget target) {
                // Reset the phone model dropdown when the vendor changes
                //_myModel.setPhoneModel(null);
                //productCountDropDown.setChoices();
                //target.addComponent(productCountDropDown);
            }
        });
        return newProductCountDropDown;
    }
}
